/**
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// uint64_t EcmaResolveSecondArgument(uint64_t function_tagged_value);
.extern EcmaResolveSecondArgument

// handle ecma.call0dyn
// regs set as follow
// x9 - frame.vregs, x10 - insn_ptr, x12 - method, x15 - acc (functional object), x13, x14 - temp

    ldr w14, [x15, JSFUNCTION_FUNCTION_INFO_FLAG_OFFSET]
    tbz w14, JSFUNCTION_FUNCTION_INFO_FLAG_IS_STRICT_START_BIT, 1f
    // (x14) <- resolved this
    mov x14, #TAGGED_VALUE_UNDEFINED
    b 2f

1:
    mov x0, x15
    stp x12, x15, [sp, #-16]!
    bl EcmaResolveSecondArgument
    ldp x12, x15, [sp], #16
    // (x14) <- resolved this
    mov x14, x0

2:
    // ABI arg reg 0 (x0) <- ark::Method*
    mov x0, x12

    // ABI arg reg 1 (x1) <- number of arguments
    mov x1, #3  // mandatory params

    // Reserve stack args
    sub sp, sp, (4 * FRAME_VREGISTER_SIZE)

    // ABI arg stack <- functional object
    str x15, [sp, 0 * FRAME_VREGISTER_SIZE]

    // ABI arg stack <- new.target
    mov x2, #TAGGED_VALUE_UNDEFINED
    str x2, [sp, 1 * FRAME_VREGISTER_SIZE]

    // ABI arg stack <- resolved this
    str x14, [sp, 2 * FRAME_VREGISTER_SIZE]

    b .Linvoke
